import { httpTokenRequest } from '@/utils/http.js';
import { getUserInfo } from '@/api/my.js'
export default {
	data() {
		return {
			focus: true, //input焦点，用于键盘隐藏后重新唤起
			form: {
				mobile: '', //	是	string	手机号
				captcha: '', //这是用户输入的验证码
				app_version: '' //版本号
			},
			codeColor: '#313131', //自定义光标的颜色
			countDown: 60, //新获取验证码的倒计时
			clock: null, //倒计时计时器
		};
	},
	methods: {
		goBack() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 键盘隐藏后设置失去焦点
		blur() {
			this.focus = false;
		},
		// 点击自定义光标显示键盘
		codeFocus(e) {
			if (e == this.form.captcha.length) {
				this.focus = true;
			}
		},
		// 输入验证码
		codeNum(event) {
			var that = this;
			var code = event.target.value;
			if (code.length != 4) {
				return false;
			}
			this.form.captcha = code;
			this.form.cid = uni.getStorageSync('cid');
			httpTokenRequest({
				methods: 'POST',
				url: '/api/user/mobilelogin',
				params: this.form
			})
				.then((res) => {
					if (res.code == 1) {
						this.$tools.showToast('登录成功');
						uni.setStorageSync('token', res.data.token);
						uni.removeStorageSync('lastOpenTime');
						// 获取个人信息  存到本地
						getUserInfo().then(res => {
							uni.setStorageSync('userInfo', res.data)
						})

						uni.switchTab({
							url: '../home/index'
						});
						this.$zegoZim.zegoLogin();
					} else {
						that.codeColor = '#ff0000';
						setTimeout(function () {
							that.form.captcha = [];
							event.target.value = '';
							that.codeColor = '#313131';
						}, 1500);
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					this.$tools.showToast(err);
				});
		},
		anewSend() {
			var data = {
				mobile: this.form.mobile, //	是	string	手机号
				event: 'mobilelogin' //是	string	事件:register=注册,mobilelogin=验证码登录,changemobile=更改手机号,resetpwd=找回密码
			};

			httpTokenRequest({
				methods: 'POST',
				url: this.$paths.smssend,
				params: data
			})
				.then((res) => {
					if (res.code == 1) {
						this.$tools.showToast('短信发送成功');
						clearInterval(this.clock);
						//重新开启倒计时
						this.countDown = 60;
						this.clock = setInterval((res) => {
							if (this.countDown > 0) {
								this.countDown--;
							} else {
								this.countDown = 60;
								clearInterval(this.clock);
							}
						}, 1000);
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},
	},
	onLoad(options) {
		this.form.mobile = options.tel;
		this.form.app_version = uni.getStorageSync('version');
		this.clock = setInterval((res) => {
			if (this.countDown > 0) {
				this.countDown--;
			} else {
				this.countDown = 60;
				clearInterval(this.clock);
			}
		}, 1000);
	},
}